using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using WP.Controls;
namespace WP.Controls.GridColumn
{


    //public class TallyColumnPc_Len : DataGridViewColumn
    //{

    //    private static TallyText.TallyType m_TallyStlye = TallyText.TallyType.Pc_Len;
    //    private static decimal m_FixLen = 0;
    //    private static decimal m_FixWidth = 0;

    //    public TallyText.TallyType TallyStlye
    //    {
    //        get
    //        {
    //            return m_TallyStlye;
    //        }
    //        set
    //        {
    //            m_TallyStlye = value;
    //        }
    //    }

    //    public decimal FixLen
    //    {
    //        get
    //        {
    //            return m_FixLen;
    //        }
    //        set
    //        {
    //            m_FixLen = value;

    //        }
    //    }

    //    public decimal FixWidth
    //    {
    //        get
    //        {
    //            return m_FixWidth;
    //        }
    //        set
    //        {
    //            m_FixWidth = value;
    //        }
    //    }




    //   public TallyColumnPc_Len(TallyText.TallyType sType, decimal tallyFixLength, decimal tallyFixWidth)
    //        : base(new TallyCellPc_Len())
    //    {

    //        m_TallyStlye = sType;
    //        m_FixLen = tallyFixLength;
    //        m_FixWidth = tallyFixWidth;
    //    }
    //    public TallyColumnPc_Len()
    //        : base(new TallyCellPc_Len())
    //    {

    //    }

    //    public override DataGridViewCell CellTemplate
    //    {
    //        get
    //        {
    //            return base.CellTemplate;
    //        }
    //        set
    //        {
    //            // Ensure that the cell used for the template is a TallyCell.
    //            if (value != null &&
    //                !value.GetType().IsAssignableFrom(typeof(TallyCellPc_Len)))
    //            {
    //                throw new InvalidCastException("Must be a TallyCells");
    //            }
    //            base.CellTemplate = value;
    //        }

    //    }
    //}

    //public class TallyColumnPc_Len_Width : DataGridViewColumn
    //{

    //    private static TallyText.TallyType m_TallyStlye = TallyText.TallyType.Pc_Len_Width ;
    //    private static decimal m_FixLen = 0;
    //    private static decimal m_FixWidth = 0;

    //    public TallyText.TallyType TallyStlye
    //    {
    //        get
    //        {
    //            return m_TallyStlye;
    //        }
    //        set
    //        {
    //            m_TallyStlye = value;
    //        }
    //    }

    //    public decimal FixLen
    //    {
    //        get
    //        {
    //            return m_FixLen;
    //        }
    //        set
    //        {
    //            m_FixLen = value;
                
    //        }
    //    }

    //    public decimal FixWidth
    //    {
    //        get
    //        {
    //            return m_FixWidth;
    //        }
    //        set
    //        {
    //            m_FixWidth = value;
    //        }
    //    }




    //    public TallyColumnPc_Len_Width(TallyText.TallyType sType, decimal tallyFixLength, decimal tallyFixWidth)
    //        : base(new TallyCellPc_Len_Width())
    //    {

    //        m_TallyStlye = sType;
    //        m_FixLen = tallyFixLength;
    //        m_FixWidth = tallyFixWidth;
    //    }
    //    public TallyColumnPc_Len_Width()
    //        : base(new TallyCellPc_Len_Width())
    //    {

    //    }



    //    public override DataGridViewCell CellTemplate
    //    {
    //        get
    //        {
    //            return base.CellTemplate;
    //        }
    //        set
    //        {
    //            // Ensure that the cell used for the template is a TallyCell.
    //            if (value != null &&
    //                !value.GetType().IsAssignableFrom(typeof(TallyCellPc_Len_Width)))
    //            {
    //                throw new InvalidCastException("Must be a TallyCells");
    //            }
    //            base.CellTemplate = value;
    //        }

    //    }
    //}

    //public class TallyCellPc_Len : DataGridViewTextBoxCell
    //{

    //    public TallyCellPc_Len()
    //        : base()
    //    {

    //    }

    //    public override void InitializeEditingControl(int rowIndex, object
    //        initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
    //    {
    //        // Set the value of the editing control to the current cell value.
    //        base.InitializeEditingControl(rowIndex, initialFormattedValue,
    //            dataGridViewCellStyle);
    //        TallyEditingControl ctl =
    //            DataGridView.EditingControl as TallyEditingControl;
    //        ctl.TallyStlye = ((TallyColumnPc_Len)this.DataGridView.Columns[this.ColumnIndex]).TallyStlye ;// m_TallyStlye;
    //        ctl.FixLen = ((TallyColumnPc_Len)this.DataGridView.Columns[this.ColumnIndex]).FixLen;// m_FixLen;
    //        ctl.FixWidth = ((TallyColumnPc_Len)this.DataGridView.Columns[this.ColumnIndex]).FixWidth;//  m_FixWidth;
    //        ctl.Value = (string)this.Value;

    //    }

    //    public override Type EditType
    //    {
    //        get
    //        {
    //            // Return the type of the editing contol that TallyCell uses.
    //            return typeof(TallyEditingControl);
    //        }
    //    }

    //    public override Type ValueType
    //    {
    //        get
    //        {
    //            // Return the type of the value that TallyCell contains.
    //            return typeof(string);
    //        }
    //    }


    //    protected override void OnKeyDown(KeyEventArgs e, int rowIndex)
    //    {

    //    }

    //    protected override void OnKeyPress(KeyPressEventArgs e, int rowIndex)
    //    {
    //        if (e.KeyChar == (char)Keys.Return)
    //            e.Handled = true;
    //    }

    //}

    //public class TallyCellPc_Len_Width : DataGridViewTextBoxCell
    //{

    //    ////private static TallyText.TallyType m_TallyStlye = TallyText.TallyType.Pc_Len_Width ;
    //    ////private static decimal m_FixLen = 0;
    //    ////private static decimal m_FixWidth = 0;




    //    ////public TallyCellPc_Len_Width(TallyText.TallyType sType, decimal tallyFixLength, decimal tallyFixWidth)
    //    ////    : base()
    //    ////{

    //    ////    m_TallyStlye = sType;
    //    ////    m_FixLen = tallyFixLength;
    //    ////    m_FixWidth = tallyFixWidth;
    //    ////}
    //    public TallyCellPc_Len_Width()
    //        : base()
    //    {

    //        //// Use the short date format.
    //        ////this.Style.Format = "d";
    //    }


    //    public override void InitializeEditingControl(int rowIndex, object
    //        initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
    //    {
    //        // Set the value of the editing control to the current cell value.
    //        base.InitializeEditingControl(rowIndex, initialFormattedValue,
    //            dataGridViewCellStyle);
    //        TallyEditingControl ctl =
    //            DataGridView.EditingControl as TallyEditingControl;
    //        ctl.TallyStlye = ((TallyColumnPc_Len_Width)this.DataGridView.Columns[this.ColumnIndex]).TallyStlye;//  m_TallyStlye;
    //        ctl.FixLen = ((TallyColumnPc_Len_Width)this.DataGridView.Columns[this.ColumnIndex]).FixLen;// m_FixLen;
    //        ctl.FixWidth = ((TallyColumnPc_Len_Width)this.DataGridView.Columns[this.ColumnIndex]).FixWidth;// m_FixWidth;
    //        ctl.Value = (string)this.Value;


    //    }

    //    public override Type EditType
    //    {
    //        get
    //        {
    //            // Return the type of the editing contol that TallyCell uses.
    //            return typeof(TallyEditingControl);
    //        }
    //    }

    //    public override Type ValueType
    //    {
    //        get
    //        {
    //            // Return the type of the value that TallyCell contains.
    //            return typeof(string);
    //        }
    //    }


    //    protected override void OnKeyDown(KeyEventArgs e, int rowIndex)
    //    {

    //    }

    //    protected override void OnKeyPress(KeyPressEventArgs e, int rowIndex)
    //    {
    //        if (e.KeyChar == (char)Keys.Return)
    //            e.Handled = true;
    //    }

    //}



    public class TallyColumn : DataGridViewColumn
    {

        private  TallyText.TallyType m_TallyStlye = TallyText.TallyType.Pc_Len;
        private  decimal m_FixLen = 0;
        private  decimal m_FixWidth = 0;
        private int m_FixPc = 0;

        public TallyText.TallyType TallyStlye
        {
            get
            {
                return m_TallyStlye;
            }
            set
            {
                m_TallyStlye = value;
            }
        }

        public decimal FixLen
        {
            get
            {
                return m_FixLen;
            }
            set
            {
                m_FixLen = value;

            }
        }

        public decimal FixWidth
        {
            get
            {
                return m_FixWidth;
            }
            set
            {
                m_FixWidth = value;
            }
        }
        public int FixPc
        {
            get
            {
                return m_FixPc;
            }
            set
            {
                m_FixPc= value;
            }
        }

        public TallyColumn(TallyText.TallyType sType,int tallyFixPc, decimal tallyFixLength, decimal tallyFixWidth)
            : base(new TallyCell())
        {

            m_TallyStlye = sType;
            m_FixPc = tallyFixPc;
            m_FixLen = tallyFixLength;
            m_FixWidth = tallyFixWidth;
        }
        public TallyColumn()
            : base(new TallyCell())
        {

        }

        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                // Ensure that the cell used for the template is a TallyCell.
                if (value != null &&
                    !value.GetType().IsAssignableFrom(typeof(TallyCell)))
                {
                    throw new InvalidCastException("Must be a TallyCells");
                }
                base.CellTemplate = value;
            }

        }
    }


    public class TallyCell : DataGridViewTextBoxCell
    {

        public TallyCell()
            : base()
        {

        }

        public override void InitializeEditingControl(int rowIndex, object
            initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            // Set the value of the editing control to the current cell value.
            base.InitializeEditingControl(rowIndex, initialFormattedValue,
                dataGridViewCellStyle);
            TallyEditingControl ctl =
                DataGridView.EditingControl as TallyEditingControl;
            ctl.TallyStlye = ((TallyColumn)this.DataGridView.Columns[this.ColumnIndex]).TallyStlye;
            ctl.FixLen = ((TallyColumn)this.DataGridView.Columns[this.ColumnIndex]).FixLen;
            ctl.FixWidth = ((TallyColumn)this.DataGridView.Columns[this.ColumnIndex]).FixWidth;
            ctl.FixPc = ((TallyColumn)this.DataGridView.Columns[this.ColumnIndex]).FixPc;
            ctl.Value = (string)this.Value;

        }

        public override Type EditType
        {
            get
            {
                // Return the type of the editing contol that TallyCell uses.
                return typeof(TallyEditingControl);
            }
        }

        public override Type ValueType
        {
            get
            {
                // Return the type of the value that TallyCell contains.
                return typeof(string);
            }
        }


        protected override void OnKeyDown(KeyEventArgs e, int rowIndex)
        {

        }

        protected override void OnKeyPress(KeyPressEventArgs e, int rowIndex)
        {
            if (e.KeyChar == (char)Keys.Return)
                e.Handled = true;
        }

    }


    public class TallyEditingControl : WoodTally , IDataGridViewEditingControl
    {
        DataGridView dataGridView;
        private bool valueChanged = false;
        int rowIndex;

        public TallyEditingControl()
        {
            //this.Format = DateTimePickerFormat.Short;
           // base.BorderStyle = Data  ; 
        }

        // Implements the IDataGridViewEditingControl.EditingControlFormattedValue 
        // property.
        public object EditingControlFormattedValue
        {
            get
            {
                return this.Value;
            }
            set
            {
                String newValue = value as String;
                if (newValue != null)
                {
                    this.Value = newValue;
                }
            }
        }

        // Implements the 
        // IDataGridViewEditingControl.GetEditingControlFormattedValue method.
        public object GetEditingControlFormattedValue(
            DataGridViewDataErrorContexts context)
        {
            return EditingControlFormattedValue;
        }

        // Implements the 
        // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
        public void ApplyCellStyleToEditingControl(
            DataGridViewCellStyle dataGridViewCellStyle)
        {
            //this.Font = dataGridViewCellStyle.Font;
            //this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
            //this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
        }

        // Implements the IDataGridViewEditingControl.EditingControlRowIndex 
        // property.
        public int EditingControlRowIndex
        {
            get
            {
                return rowIndex;
            }
            set
            {
                rowIndex = value;
            }
        }

        // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey 
        // method.
        public bool EditingControlWantsInputKey(
            Keys key, bool dataGridViewWantsInputKey)
        {
            //this.dataGridView.EndEdit();  
            // Let the DateTimePicker handle the keys listed.
            switch (key & Keys.KeyCode)
            {
                case Keys.Left:
                case Keys.Up:
                case Keys.Down:
                case Keys.Right:
                    return true;
                case Keys.Home:
                case Keys.End:
                case Keys.PageDown:
                case Keys.Return:
                case Keys.PageUp:
                   // return true;
                default:
                    return false;
            }
        }


        // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit 
        // method.
        public void PrepareEditingControlForEdit(bool selectAll)
        {
            // No preparation needs to be done.
        }

        // Implements the IDataGridViewEditingControl
        // .RepositionEditingControlOnValueChange property.
        public bool RepositionEditingControlOnValueChange
        {
            get
            {
                return false;
            }
        }

        // Implements the IDataGridViewEditingControl
        // .EditingControlDataGridView property.
        public DataGridView EditingControlDataGridView
        {
            get
            {
                return dataGridView;
            }
            set
            {
                dataGridView = value;
            }
        }

        // Implements the IDataGridViewEditingControl
        // .EditingControlValueChanged property.
        public bool EditingControlValueChanged
        {
            get
            {
                return valueChanged;
            }
            set
            {
                valueChanged = value;
            }
        }

        // Implements the IDataGridViewEditingControl
        // .EditingPanelCursor property.
        public Cursor EditingPanelCursor
        {
            get
            {
                return base.Cursor;
            }
        }

       protected override void OnValueChanged(WoodTallyEventArgs eventargs)
       {
           // Notify the DataGridView that the contents of the cell
           // have changed.
           
           
           valueChanged = true;
           this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
        //   this.dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
           base.OnValueChanged(eventargs);
           
         //  this.dataGridView.EndEdit();  
           
       }
    }
}
